package com.ayoka.mvp;

import com.ayoka.R;
import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.mvp.CourseAssignmentVideoMVP.CourseAssignmentVideoModel.SubmitCourseAssignmentVideoAnswerBean;
import com.ayoka.mvp.CourseAssignmentVideoMVP.CourseAssignmentVideoModel.SubmitCourseAssignmentBean;
import com.ayoka.mvp.CourseAssignmentVideoMVP.CourseAssignmentVideoModel.RequestCourseAssignmentVideoRecordBean;
import com.ayoka.mvp.CourseAssignmentVideoMVP.CourseAssignmentVideoModel.VideoQuestionResultBean;
import com.google.gson.Gson;
import com.jcp.baselibrary.okhttp3.RequestBean;
import com.jcp.baselibrary.utils.UIUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface CourseAssignmentVideoMVP {

    interface CourseAssignmentVideoModel {
        class SubmitCourseAssignmentVideoAnswerBean {
            public String VoiceUrl;
        }

        class SubmitCourseAssignmentBean {
            public int task_completed_status;
        }

        class RequestCourseAssignmentVideoRecordBean extends RequestBean{
            public int CourseID;
            public int AssignmentID;
            public List<VideoQuestionResultBean> Result;

            @Override
            public String toString() {
                return new Gson().toJson(this);
            }
        }

        class SubmitCourseAssignmentVideoRecordBean {
            public int ID;
            public int Helpful;
            public int DiaryIsPrivate;
            public String Diary;

            public int TaskID;
            public int CourseID;
            public int AssignmentID;
            public List<VideoQuestionResultBean> Result;

            @Override
            public String toString() {
                return new Gson().toJson(this);
            }
        }

        class VideoQuestionResultBean {
            public int QuestionID;
            public String VoiceUrl;
            public long VoiceDuration;
        }
    }

    interface ICourseAssignmentVideoView extends IBaseView {

        void onSubmitCourseAssignmentVideoAnswerSuccess(SubmitCourseAssignmentVideoAnswerBean data);

        void onSubmitCourseAssignmentSuccess(SubmitCourseAssignmentBean data);
    }

    class CourseAssignmentVideoPresenter extends BasePresenter<ICourseAssignmentVideoView> {

        public void postCourseAssignmentVideoAnswer(File audioFile) {
            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.submitCourseAssignmentVideoTypeAnswer(this, audioFile, new OkHttpListener<BaseResponse<SubmitCourseAssignmentVideoAnswerBean>>() {
                @Override
                public void onSuccess(BaseResponse<SubmitCourseAssignmentVideoAnswerBean> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onSubmitCourseAssignmentVideoAnswerSuccess(response.data);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        public void postCourseAssignmentVideoRecord(CourseAssignmentBean courseAssignmentBean) {
            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.submitCourseAssignmentVideoTypeRecord(this, getRequestBean(courseAssignmentBean), new OkHttpListener<BaseResponse<SubmitCourseAssignmentBean>>() {
                @Override
                public void onSuccess(BaseResponse<SubmitCourseAssignmentBean> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onSubmitCourseAssignmentSuccess(response.data);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        private RequestCourseAssignmentVideoRecordBean getRequestBean( CourseAssignmentBean courseAssignmentBean) {
            RequestCourseAssignmentVideoRecordBean requestBean = new RequestCourseAssignmentVideoRecordBean();
            requestBean.CourseID = courseAssignmentBean.CourseID;
            requestBean.AssignmentID = courseAssignmentBean.ID;
            requestBean.Result = new ArrayList<>();
            for (CourseAssignmentBean.VideoQuestionBean videoQuestion : courseAssignmentBean.VideoQuestions) {
                if (videoQuestion.QuestionType == 1){
                    VideoQuestionResultBean resultBean = new VideoQuestionResultBean();
                    resultBean.QuestionID = videoQuestion.ID;
                    resultBean.VoiceUrl = videoQuestion.answer;
                    resultBean.VoiceDuration = videoQuestion.duration;
                    requestBean.Result.add(resultBean);
                }
            }
            return requestBean;
        }
    }

}
